package com.ruin.tech.ahri.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author luofan
 * @date 2019/12/06 14:21:09
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config implements WebMvcConfigurer {
    @Value("${spring.application.name}")
    private String serviceName;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .select()
                //指定生成API文档的包名 可以使用any()或 none
                .apis(RequestHandlerSelectors.basePackage("com.ruin.tech.ahri.controller"))
                //any() 所有的生成api接口文档 none() 都不生成api regex()正则匹配
                .paths(PathSelectors.regex("/user/.*|/article/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //标题
                .title(getSwaggerTitle(serviceName))
                //公开的API的联系信息
                .contact(new Contact("itruin", "https://www.itruin.tech", "itruin@163.com"))
                //版本号
                .version("版本号:1.0.0")
                //应用程序的简短描述。GFM语法可用于富文本表示
                .description("描述:前后端分离，API接口文档")
                //许可证书
                .license("")
                .licenseUrl("")
                //服务条款URL
                .termsOfServiceUrl("")
                .build();
    }

    private String getSwaggerTitle(String sm) {
        List<String> list = Stream.of(SwaggerEnum.values()).filter(t->Objects.equals(sm, t.getServiceName())).map(SwaggerEnum::getSwaggerTitle).collect(Collectors.toList());
        return ObjectUtils.isEmpty(list)?SwaggerEnum.OTHER.getSwaggerTitle():list.get(0);
   }

   @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

